package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"multi-ctrl-sys/cache"
	"multi-ctrl-sys/models"
	"strconv"
	"time"
)

func Good(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	good_price, _ := strconv.Atoi(c.PostForm("good_price"))

	data := models.Good{
		GoodCode:  good_code,
		GoodName:  good_name,
		GoodPrice: good_price,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func GoodOrder(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_peo := c.PostForm("order_peo")
	order_type := c.PostForm("order_type")
	gid, _ := strconv.Atoi(c.PostForm("gid"))

	data := models.GoodOrder{
		OrderCode: order_code,
		OrderPeo:  order_peo,
		OrderType: order_type,
		Gid:       gid,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func GoodOrderList(c *gin.Context) {
	//SELECT
	//goods.good_code as GoodCode,
	//goods.good_name as GoodName,
	//goods.good_price as GoodPrice,
	//good_orders.order_code as OrderCode,
	//good_orders.order_peo as OrderPeo,
	//good_orders.order_type as OrderType
	//FROM
	//good_orders
	//LEFT JOIN goods ON goods.id = good_orders.gid

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	redis_key := "list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	var list []models.GoodOrderList
	if cacheData == "" {
		models.DB.
			Table("good_orders").
			Select(
				"goods.good_code as GoodCode",
				"goods.good_name as GoodName",
				"goods.good_price as GoodPrice",
				"good_orders.order_code as OrderCode",
				"good_orders.order_peo as OrderPeo",
				"good_orders.order_type as OrderType",
			).
			Offset(offset).
			Limit(size).
			Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Order("good_price asc").
			Debug().
			Find(&list)

		jsonData, _ := json.Marshal(list)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64

	models.DB.Model(&models.GoodOrder{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  list,
		"total": total,
	})
	return

}

func GoodOrderDetail(c *gin.Context) {
	id := c.Query("id")

	redis_key := "detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	var detail models.GoodOrderList
	if cacheData == "" {
		models.DB.
			Table("good_orders").
			Select(
				"goods.good_code as GoodCode",
				"goods.good_name as GoodName",
				"goods.good_price as GoodPrice",
				"good_orders.order_code as OrderCode",
				"good_orders.order_peo as OrderPeo",
				"good_orders.order_type as OrderType",
			).
			Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Where("good_orders.id = ?", id).
			Order("good_price asc").
			Debug().
			Find(&detail)

		jsonData, _ := json.Marshal(detail)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &detail)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": detail,
	})
	return

}
